﻿
using System.Globalization;

#if WINDOWS

using System.Windows.Data;

namespace KinonekoSoftware.Foundation.UI.Converters
{


    public class BooleanToVisibilityConverter : IValueConverter
    {
        public static readonly object Visible   = Visibility.Visible;
        public static readonly object Collapsed = Visibility.Collapsed;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var v = value is bool b && b;
            
            if (IsTrueToVisible)
            {
                return v ? Visible : Collapsed;
            }
            return v ? Collapsed : Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is Visibility v)
            {
                return v == Visibility.Visible ? Boxing.Box(IsTrueToVisible) : Boxing.Box(!IsTrueToVisible) ;
            }

            return Boxing.False;
        }
        
        public bool IsTrueToVisible { get; set; }
    }

    public class BooleanToVisibility2Converter : IValueConverter
    {
        public static readonly object Visible   = Visibility.Visible;
        public static readonly object Collapsed = Visibility.Hidden;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var v = value is bool b && b;
            
            if (IsTrueToVisible)
            {
                return v ? Visible : Collapsed;
            }
            return v ? Collapsed : Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is Visibility v)
            {
                return v == Visibility.Visible ? Boxing.Box(IsTrueToVisible) : Boxing.Box(!IsTrueToVisible) ;
            }

            return Boxing.False;
        }
        
        public bool IsTrueToVisible { get; set; }
    }
}

namespace KinonekoSoftware.Foundation.UI.Converters
{
    partial class ConverterPool
    {
        public static IValueConverter TrueToVisibility { get; } = new BooleanToVisibilityConverter{ IsTrueToVisible = true };
        public static IValueConverter FalseToVisibility { get; } = new BooleanToVisibilityConverter{ IsTrueToVisible = false };
        public static IValueConverter TrueToHidden { get; } = new BooleanToVisibility2Converter{ IsTrueToVisible = false };
        public static IValueConverter FalseToHidden { get; } = new BooleanToVisibility2Converter{ IsTrueToVisible = true };
    }
}


#else

namespace KinonekoSoftware.Foundation.UI.Converters
{


    public class BooleanToVisibilityConverter : ConverterBase
    {

        public bool IsTrueToVisible { get; set; }
        
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var v = value is bool b && b;
            
            if (IsTrueToVisible)
            {
                return Boxing.Box(v);
            }
            
            return Boxing.Box(!v);
        }
    }

}

namespace KinonekoSoftware.Foundation.UI.Converters
{
    partial class ConverterPool
    {
        public static IValueConverter TrueToVisibility { get; } = new BooleanToVisibilityConverter{ IsTrueToVisible = true };
        public static IValueConverter FalseToVisibility { get; } = new BooleanToVisibilityConverter{ IsTrueToVisible = false };
    }
}
#endif